//
//  ViewController.m
//  KVODemo
//
//  Created by 牛萌 on 15/2/28.
//  Copyright (c) 2015年  Aster0id. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, weak) IBOutlet UILabel *label;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver:self forKeyPath:@"clickCount" options:NSKeyValueObservingOptionNew context:nil];
    self.clickCount = 0;
}

-(void)dealloc {
    [self removeObserver:self forKeyPath:@"clickCount"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:@"clickCount"]) {
        NSInteger count = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
        self.label.text = [NSString stringWithFormat:@"点击次数: %zd",count];
    }
}

- (IBAction)clickButton:(UIButton *)sender {
    self.clickCount ++;
}

@end
